<template>
  <div class="index">
    <van-card v-for="(goods, index) in hotGoodsList" :key="index" :num="goods.stock" :price="goods.price" :desc="goods.descript" :title="goods.name" :thumb="goods.imgUrl" @click="viewGoodsDetail(goods.id)" />
  </div>
</template>

<script>
import card from "@/components/card";
import utils from "@/utils/index.js";
export default {
  data() {
    return {
      hotGoodsList: []
    };
  },
  components: {
    card
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "热门商品"
    })
  },
  methods: {
    viewGoodsDetail(id) {

      const url = "../goods/index?id=" + id;
      wx.navigateTo({ url });
    },
    getHotGoodsList() {

      // 获取热门商品
      this.$API.get("goods/searchHot").then(res => {
        let tmpHotGoodsList = res.data;

        for (let i = 0; i < tmpHotGoodsList.length; i++) {
          const e = tmpHotGoodsList[i];
          let t = {
            id: e.id,
            stock: e.stock,
            price: utils.formatPrice(e.price),
            descript: e.descript,
            name: e.name,
            imgUrl: utils.fileMgrUrl + e.pic
          };

          this.hotGoodsList.push(t);
        }
      });
    }
  },

  created() { },

  onLoad() {
    this.hotGoodsList= [];
    this.getHotGoodsList();
  }
};
</script>

<style>
swiper {
  height: 220px;
}
swiper-item > navigator > image {
  width: 100%;
}
</style>
